package com.lg.partition29;

import java.io.*;
import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2022/4/4 10:26
 */
public class P2863USACO06JAN_TheCowPromS {

    private static final int N = 20010, M = 50010;
    private static int[] h = new int[N], e = new int[M], ne = new int[M];
    private static int[] dfn = new int[N], low = new int[N], stk = new int[N], scc = new int[N], size = new int[N];
    private static boolean[] st = new boolean[N];
    private static int n, m, idx = 0, timestamp = 0, top = 0, sccId = 0;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] ss = reader.readLine().split(" ");
        n = Integer.parseInt(ss[0]);
        m = Integer.parseInt(ss[1]);
        Arrays.fill(h, -1);
        for (int i = 0; i < m; i++) {
            ss = reader.readLine().split(" ");
            int a = Integer.parseInt(ss[0]), b = Integer.parseInt(ss[1]);
            add(a, b);
        }
        writer.write(resolve() + "\n");
        writer.flush();
    }

    private static int resolve() {
        for (int i = 1; i <= n; i++) {
            if (dfn[i] == 0) tarjan(i);
        }
        int ans = 0;
        for (int i = 1; i <= sccId; i++) {
            if (size[i] > 1) ans++;
        }
        return ans;
    }

    private static void tarjan(int u) {
        dfn[u] = low[u] = ++timestamp;
        stk[++top] = u;
        st[u] = true;
        for (int i = h[u]; i != -1; i = ne[i]) {
            int v = e[i];
            if (dfn[v] == 0) {
                tarjan(v);
                low[u] = Math.min(low[u], low[v]);
            } else if (st[v]) {
                low[u] = Math.min(low[u], dfn[v]);
            }
        }
        if (dfn[u] == low[u]) {
            sccId++;
            int v = -1;
            do {
                v = stk[top--];
                st[v] = false;
                scc[v] = sccId;
                size[sccId]++;
            } while (u != v);
        }
    }

    private static void add(int a, int b) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx++;
    }
}
